/*
global_proc.dm
All of the procs that are needed across the board can be found here. Some make use of text manipulation,
others are for formatting purposes, and others serve different purposes yet.
*/

proc
	weighted_rand(choices[], weights[])
		if(!choices || !weights) return null

		//Build a range of weights
		var/max_num = 0
		for(var/X in weights) if(isnum(X)) max_num += X

		//Now roll in the range.
		var/weighted_num = rand(1,max_num)

		var/running_total, i

		//Loop through all possible choices
		for(i = 1; i <= choices.len; i++)
			if(i > weights.len) return null

			running_total += weights[i]

			//Once the current step is less than the roll,
			// we have our winner.
			if(weighted_num <= running_total)
				return choices[i]

	gen_uid(string)
		return md5("[world.time][string][world.realtime]")

	winshow_center(mob/c, window)
		winshow(c, window)
		winset(c, window, "pos=100,100")

		#ifdef DEBUG_MODE
	debug(text)
		world << "<b>\[Debug\] [text]</b>"
		#endif

	//cut out everything that's not alpha, and capitalize the first letter
	make_proper(text)
		for(var/o=1 to lentext(text))
			var/ascii=text2ascii(copytext(text,o,++o))
			if((ascii>=65&&ascii<=90)||(ascii>=97&&ascii<=122))
				.+=ascii2text(ascii)
		.=html_encode(ckeyEx(ckey(.)))
		var/first=uppertext(copytext(.,1,2))
		first+=lowertext(copytext(.,2,0))
		.=first

	//format a message to append [server] to the beginning
	format_server(text)
		.="<b>\[Server\] [text]</b>"

	speak_local(mob/m, text)
		range(10,m) << "\[Local\] <b>[m.name]</b>: [html_encode(text)]</font>"

	//check if there is no text, just spaces
	false_text(text)
		var/x=0
		while(++x<=lentext(text))
			if(copytext(text,x,x+1)!=" ")
				return FALSE

		return TRUE

	//make a new type of monster, given the monster type and where to make it
	make_new_monster(monster/type,turf/loca,time)
		if(!loca)return
		spawn(time)
			new type(loca)

	//sort a list based on the name
	SortList(list/selected)
		for(var/S in selected)
			var/obj/builditems/B=S
			var/obj/builditems/A=new B
			A.name=A.aname
			selected.Remove(S)
			A.saveable=0
			selected.Add(A)
		for(var/obj/A in selected)
			if(A.name=="")
				del(A)

	cone(atom/center = usr, dir = NORTH, list/list = view(center))
		for(var/atom/O in list) if(!O.InCone(center, dir)) list -= O
		return list

	Query(qString)

		var
			DBI = "dbi:mysql:[sql_db]:[sql_ip]:[sql_port]"
			DBConnection/my_connection = new()
			connected = my_connection.Connect(DBI,sql_user,sql_pass)

		if(!connected)
			world.log << "No connection to the database, good luck saving. ([my_connection.ErrorMsg()])"
			world << my_connection.ErrorMsg()
								//a common error: The specified module could not be found
								//this means you need to download libmysql.dll and place
								//it in your system32 folder

		var/DBQuery/qry = my_connection.NewQuery(qString)
		qry.Execute()

//		if(qry.ErrorMsg())
//			world.log << qry.ErrorMsg()

		return qry